package com.yjd.back.contentmanager;

import com.yjd.comm.base.control.BaseControl;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.tag.model.TagModel;
import com.yjd.comm.tag.service.ITagService;
import com.yjd.comm.util.FrameUtil;
import com.yjd.comm.util.ServletUtil;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by yjd-wap on 2017/7/10.
 */
@Controller
@Scope("prototype")
@RequestMapping("/tag/")
public class TagControl extends BaseControl {

    @Resource
    private ITagService tagService;

    /**
     * 标签列表
     *
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("list")
    public String list(HttpServletRequest request, HttpServletResponse response) throws Exception {
        List<TagModel> list = this.tagService.getModelList(FrameUtil.newHashMap("sort", " tag_id desc "), DbWREnums.READ);
        request.setAttribute("list", list);
        return "tag.list";
    }

    /**
     * 跳转到新增页面
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("toadd")
    public String toadd() throws Exception {
        return "tag.toaddoredit";
    }


    /**
     * 跳转到编辑页面
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("toedit/{tag_id}")
    public String toedit(@PathVariable("tag_id") Long tag_id)
            throws Exception {
        if (tag_id == null) {
            FrameUtil.throwBaseException1("参数有误!");
        }
        TagModel model = this.tagService.getModelById(tag_id, DbWREnums.READ);
        if (model == null) {
            return this.toErrorPage(null, "数据被删除!");
        }
        request.setAttribute("model", model);
        return "tag.toaddoredit";
    }

    /**
     * 新增和保存
     *
     * @param model
     * @throws Exception
     */
    @RequestMapping("save")
    public void save(TagModel model) throws Exception {
        Long currtime = FrameUtil.getTime(null);
        model.setLast_update_time(currtime);

        if (model.getTag_id() == null) {
            model.setCreate_time(currtime);
            this.tagService.insert(model, this.getOperParamDto());
        } else {
            this.tagService.update(model, this.getOperParamDto());
        }
        this.successMsg(ServletUtil.getUrl("/tag/list"), "保存成功!");
    }

    /**
     * 根据管理员id删除管理员
     *
     * @return
     * @throws Exception
     */
    @RequestMapping("del/{tag_id}")
    public void del(@PathVariable("tag_id") Long tag_id) throws Exception {
        if (tag_id == null) {
            FrameUtil.throwBaseException1("参数有误!");
        }
        TagModel model = new TagModel();
        model.setTag_id(tag_id);
        model.setEnable(0);
        model.setLast_update_time(FrameUtil.getTime(null));
        this.tagService.update(model, this.getOperParamDto());
        this.successMsg(null, "删除数据成功!");
    }

}